/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.UploadManager;

import api.Beans.UserLogin;
import api.action.ActionPackageClient;
import api.action.ActionPackageServer;
import api.exception.TCloudAplicationException;
import api.localModel.LocalFileStatus;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import org.omg.CORBA.portable.ApplicationException;
import tcloudclient.actionManager.ActionBuilder;
import tcloudclient.connect.ServerConnect;
import tcloudclient.contex.Context;
import tcloudclient.fileSystem.FileNode;

/**
 *
 * @author Tomas
 */
public class ConnectionManager implements ConnectionManagerInterface {

    private ServerConnect serverConnect;

    public ConnectionManager() {
        serverConnect = new ServerConnect();
    }

    @Override
    public boolean uploadChange(FileNode root) throws ConnectionException, TCloudAplicationException {
        ActionPackageServer generateActions = new ActionBuilder(root).generateActions();
        if (generateActions.getActions().size() > 0) {
            try {

                serverConnect.uploadChanges(generateActions);

                return true;
            } catch (NamingException | IOException | TCloudAplicationException ex) {
                Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
                throw new ConnectionException(ex);
            }
        } else {
            return false;
        }
    }

    @Override
    public boolean isUpdate() throws ConnectionException {
        try {
            Long userId = Context.getInstance().getApplicationSettings().getUserId();
            int accHeaderVetsion = Context.getInstance().getApplicationSettings().getAccHeaderVetsion();
            return serverConnect.isUpdate(userId, accHeaderVetsion);
        } catch (NamingException | IOException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new ConnectionException(ex);
        }
    }

    @Override
    public ActionPackageClient downloadChange(FileNode root) throws ConnectionException {
        LocalFileStatus build = new LocalFileSystemStatusBuilder().build(root);
        try {
            return serverConnect.downloadUpdateActions(build);
        } catch (NamingException | IOException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new ConnectionException(ex);
        }
    }

    @Override
    public UserLogin login(String userName, String password) throws ConnectionException {
        try {
            return serverConnect.login(userName, password);
        } catch (NamingException | IOException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new ConnectionException(ex);
        }
    }
}
